package com.spx.kernel;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import com.spx.kernel.service.CoreServiceLocator;
import com.spx.kernel.service.PersistService;

/**
 * The activator class controls the plug-in life cycle
 */
public class KernelActivator extends AbstractUIPlugin
{

	// The plug-in ID
	public static final String PLUGIN_ID = "com.spx.kernel";

	// The shared instance
	private static KernelActivator plugin;

	private PersistService persistService;

	/**
	 * The constructor
	 */
	public KernelActivator()
	{
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext
	 * )
	 */
	@Override
	public void start(BundleContext context) throws Exception
	{
		super.start(context);
		plugin = this;

		try
		{
			Application.connect();

			// getPersistService();
		} catch (final Exception e)
		{
			e.printStackTrace();

		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
	 * )
	 */
	@Override
	public void stop(BundleContext context) throws Exception
	{
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 * 
	 * @return the shared instance
	 */
	public static KernelActivator getDefault()
	{
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given plug-in
	 * relative path
	 * 
	 * @param path
	 *            the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path)
	{
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}

	public PersistService getPersistService()
	{
		if (persistService == null)
		{
			persistService = CoreServiceLocator.getInstance().getService("persist-service", PersistService.class);
			
		}
		return persistService;
	}
	
	
	 public static <T,O extends Object>  T getTransactionalProxy(O obj, Class<T> clazz){
	    return KernelActivator.getDefault().getPersistService(). getTransactionalProxy( obj, clazz);
	}
	
}
